<template>
  <div>
    <el-card>
      <div class="top">
        <el-button type="primary">新增顶级分类</el-button>
      </div>
      <div class="center">
        <el-table
          :data="categoryTable"
          border
          stripe
          style="width: 100%"
          row-key="id"
          :tree-props="{ children: 'children' }"
        >
          <el-table-column fixed width="50" label="#"> </el-table-column>
          <el-table-column prop="id" width="80" label="ID"> </el-table-column>
          <el-table-column
            prop="categoryName"
            label="顶级分类"
          ></el-table-column>
          <el-table-column prop="categoryDesc" label="描述"></el-table-column>
          <el-table-column label="编辑" width="200">
            <template v-slot="scope">
              <el-button
                type="primary"
                size="mini"
                icon="el-icon-edit"
                @click="dialogTrue(scope.row.id)"
                >新增</el-button
              >
              <el-button
                type="danger"
                size="mini"
                icon="el-icon-delete"
                @click="CateDelete(scope.row)"
                >删除</el-button
              >
            </template>
          </el-table-column>
        </el-table>
      </div>
    </el-card>
    <el-dialog title="添加分类" :visible.sync="dialogFormVisible" width="30%">
      <el-form :model="CateForm">
        <el-form-item label="分类名称">
          <el-input v-model="CateForm.categoryName"></el-input>
        </el-form-item>
        <el-form-item label="分类描述">
          <el-input
            type="textarea"
            :autosize="{ minRows: 2 }"
            v-model="CateForm.categoryDesc"
          ></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogFormVisible = false">取 消</el-button>
        <el-button type="primary" @click="CateAdd(CateForm)">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { getCateList, CateAdd } from '../api/api.js'
export default {
  data() {
    return {
      dialogFormVisible: false,
      CateForm: {
        categoryName: '',
        categoryDesc: ''
      },
      cateId: '',
      // 分类数据
      categoryTable: []
    }
  },
  created() {
    this.getCateList()
  },
  methods: {
    dialogTrue(e) {
      this.dialogFormVisible = true
      this.cateId = e
    },
    async getCateList() {
      const { data: res } = await getCateList()
      this.categoryTable = res
    },
    async CateAdd(e) {
      // 查找点击的分类
      const find = this.categoryTable.find(cate => cate.id === this.cateId)
      // 方式一，子分类名称可以相同  直接添加
      // await CateAdd({
      //   categoryName: e.categoryName,
      //   categoryDesc: e.categoryDesc,
      //   parentid: this.cateId
      // })
      // 方式二，展示二级分类，子分类名称不能相同，判断添加
      // 不存在子级 直接创建并添加子级
      if (!find.children || find.children.length < 1 || find.children.length === 0) {
        await CateAdd({
          categoryName: e.categoryName,
          categoryDesc: e.categoryDesc,
          parentid: this.cateId
        })
      }
      // 存在子级，判断是否存在相同name值,存在则提示添加失败，否则成功添加
      if (find.children) {
        const findChildren = find.children.find(cate => cate.categoryName === e.categoryName)
        if (findChildren) {
          this.$message.error('添加失败！已存在相同分类名称！')
        } else {
          await CateAdd({
            categoryName: e.categoryName,
            categoryDesc: e.categoryDesc,
            parentid: this.cateId
          })
        }
      }

      this.getCateList()
      this.CateForm = ''
      console.log(find)
      this.dialogFormVisible = false
    }
  }
}
</script>

<style lang="less" scoped>
.top {
  margin-bottom: 15px;
}
</style>
